package library.common.view.dialog;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.Map;

import library.common.view.dialog.base.BaseDialog;
import library.common.view.dialog.base.IDismissResult;
import library.common.view.dialog.base.IViewBinder;

/**
 * @author Created by qiang.hou on 2018/12/18.
 * @version 1.0
 */
public class CommonDialog extends BaseDialog {

    public CommonDialog(Context context) {
        super(context);
    }

    public CommonDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    protected CommonDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    private int layoutId = 0;

    /**
     *
     */
    private IViewBinder viewBinder;

    private IDismissResult dismissResult;

    /**
     *
     */
    private int[] clickViewIds;
    private View.OnClickListener onViewClickListener = null;

    /**
     * set key to value(View)
     */
    private Map<Integer, Integer> textIntegerMap = null;
    /**
     * set key to value(View)
     */
    private Map<String, Integer> textStringMap = null;
    /**
     * set key to value(View)
     */
    private Map<Bitmap, Integer> imageBitmapMap = null;
    /**
     * set key to value(View)
     */
    private Map<Integer, Integer> imageIntegerMap = null;
    /**
     * set key to value(View)
     */
    private Map<Drawable, Integer> imageDrawableMap = null;

    public void setLayoutId(int layoutId) {
        this.layoutId = layoutId;
    }

    public void setViewBinder(IViewBinder viewBinder) {
        this.viewBinder = viewBinder;
    }

    public void setDismissResult(IDismissResult dismissResult) {
        this.dismissResult = dismissResult;
    }

    public void setClickViewIds(int[] clickViewIds) {
        this.clickViewIds = clickViewIds;
    }

    public void setOnViewClickListener(View.OnClickListener onViewClickListener) {
        this.onViewClickListener = onViewClickListener;
    }

    public void setTextStringMap(Map<String, Integer> textStringMap) {
        this.textStringMap = textStringMap;
    }

    public void setTextResourceMap(Map<Integer, Integer> textIntegerMap) {
        this.textIntegerMap = textIntegerMap;
    }

    public void setImageBitmapMap(Map<Bitmap, Integer> imageBitmapMap) {
        this.imageBitmapMap = imageBitmapMap;
    }

    public void setImageResourceMap(Map<Integer, Integer> imageIntegerMap) {
        this.imageIntegerMap = imageIntegerMap;
    }

    public void setImageDrawableMap(Map<Drawable, Integer> imageDrawableMap) {
        this.imageDrawableMap = imageDrawableMap;
    }

    /**
     * bind text(Resource)
     *
     * @param rootView     root view
     * @param textResource text resource
     * @param viewId       view id
     */
    private void setText(View rootView, int textResource, int viewId) {
        if (null == rootView || viewId <= 0) {
            return;
        }
        View tempView = rootView.findViewById(viewId);
        if (tempView instanceof EditText) {
            ((EditText) tempView).setText(getContext().getResources().getString(textResource));
        } else if (tempView instanceof Button) {
            ((Button) tempView).setText(getContext().getResources().getString(textResource));
        } else if (tempView instanceof TextView) {
            ((TextView) tempView).setText(getContext().getResources().getString(textResource));
        }
    }

    /**
     * bind text(String)
     *
     * @param rootView root view
     * @param text     text string
     * @param viewId   view id
     */
    private void setText(View rootView, String text, int viewId) {
        if (null == rootView || viewId <= 0) {
            return;
        }
        View tempView = rootView.findViewById(viewId);
        if (tempView instanceof EditText) {
            ((EditText) tempView).setText(text);
        } else if (tempView instanceof Button) {
            ((Button) tempView).setText(text);
        } else if (tempView instanceof TextView) {
            ((TextView) tempView).setText(text);
        }
    }

    /**
     * bind image(Bitmap)
     *
     * @param rootView root view
     * @param bitmap   bitmap
     * @param viewId   view id
     */
    private void setImageBitmap(View rootView, Bitmap bitmap, int viewId) {
        if (null == rootView || null == bitmap || viewId <= 0) {
            return;
        }
        View tempView = rootView.findViewById(viewId);
        if (tempView instanceof ImageButton) {
            ((ImageButton) tempView).setImageBitmap(bitmap);
        } else if (tempView instanceof ImageView) {
            ((ImageView) tempView).setImageBitmap(bitmap);
        }
    }

    /**
     * bind image(Resource)
     *
     * @param rootView      root view
     * @param imageResource image resource id
     * @param viewId        view id
     */
    private void setImageResource(View rootView, int imageResource, int viewId) {
        if (null == rootView || imageResource <= 0 || viewId <= 0) {
            return;
        }
        View tempView = rootView.findViewById(viewId);
        if (tempView instanceof ImageButton) {
            ((ImageButton) tempView).setImageResource(imageResource);
        } else if (tempView instanceof ImageView) {
            ((ImageView) tempView).setImageResource(imageResource);
        }
    }

    /**
     * bind image(Drawable)
     *
     * @param rootView root view
     * @param drawable image drawable
     * @param viewId   view id
     */
    private void setImageDrawable(View rootView, Drawable drawable, int viewId) {
        if (null == rootView || null == drawable || viewId <= 0) {
            return;
        }
        View tempView = rootView.findViewById(viewId);
        if (tempView instanceof ImageButton) {
            ((ImageButton) tempView).setImageDrawable(drawable);
        } else if (tempView instanceof ImageView) {
            ((ImageView) tempView).setImageDrawable(drawable);
        }
    }

    @Override
    protected Object setResult() {
        return null == dismissResult ? null : dismissResult.createResult();
    }

    @Override
    protected int returnLayoutId() {
        return layoutId;
    }

    @Override
    protected void initProcess() {
    }

    @Override
    protected void onBindViewData(View view) {
        if (null != viewBinder) {
            viewBinder.bindView(view);
        } else {

            /*
             * bind click listener
             */
            if (null != clickViewIds && clickViewIds.length > 0 && null != onViewClickListener) {
                for (Integer id : clickViewIds) {
                    view.findViewById(id).setOnClickListener(onViewClickListener);
                }
            }

            /*
             * bind text(resource)
             */
            if (null != textIntegerMap && textIntegerMap.size() > 0) {
                for (Map.Entry<Integer, Integer> entry : textIntegerMap.entrySet()) {
                    setText(view, entry.getKey(), entry.getValue());
                }
            }

            /*
             * bind text(String)
             */
            if (null != textStringMap && textStringMap.size() > 0) {
                for (Map.Entry<String, Integer> entry : textStringMap.entrySet()) {
                    setText(view, entry.getKey(), entry.getValue());
                }
            }

            /*
             * bind image(Bitmap)
             */
            if (null != imageBitmapMap && imageBitmapMap.size() > 0) {
                for (Map.Entry<Bitmap, Integer> entry : imageBitmapMap.entrySet()) {
                    setImageBitmap(view, entry.getKey(), entry.getValue());
                }
            }

            /*
             * bind image(Resource)
             */
            if (null != imageIntegerMap && imageIntegerMap.size() > 0) {
                for (Map.Entry<Integer, Integer> entry : imageIntegerMap.entrySet()) {
                    setImageResource(view, entry.getKey(), entry.getValue());
                }
            }

            /*
             * bind image(Drawable)
             */
            if (null != imageDrawableMap && imageDrawableMap.size() > 0) {
                for (Map.Entry<Drawable, Integer> entry : imageDrawableMap.entrySet()) {
                    setImageDrawable(view, entry.getKey(), entry.getValue());
                }
            }
        }
    }

    public static class Builder extends BaseDialog.Builder<CommonDialog> {

        private Context mContext;

        private int layoutId = 0;

        private IViewBinder viewBinder;

        private IDismissResult dismissResult;

        /**
         *
         */
        private int[] clickViewIds;
        private View.OnClickListener onViewClickListener = null;

        /**
         * set key to value(View)
         */
        private Map<Integer, Integer> textIntegerMap = null;
        /**
         * set key to value(View)
         */
        private Map<String, Integer> textStringMap = null;
        /**
         * set key to value(View)
         */
        private Map<Bitmap, Integer> imageBitmapMap = null;
        /**
         * set key to value(View)
         */
        private Map<Integer, Integer> imageIntegerMap = null;
        /**
         * set key to value(View)
         */
        private Map<Drawable, Integer> imageDrawableMap = null;

        public Builder() {
            super();
        }

        public Builder context(Context context) {
            this.mContext = context;
            return this;
        }

        public Builder layoutId(int layoutId) {
            this.layoutId = layoutId;
            return this;
        }

        public Builder bindView(IViewBinder binder) {
            viewBinder = binder;
            return this;
        }

        public Builder setDismissResult(IDismissResult dismissResult) {
            this.dismissResult = dismissResult;
            return this;
        }

        public Builder setOnClickListener(View.OnClickListener listener, int... viewIds) {
            this.clickViewIds = viewIds;
            this.onViewClickListener = listener;
            return this;
        }

        public Builder setImageBitmap(Map<Bitmap, Integer> mapping) {
            this.imageBitmapMap = mapping;
            return this;
        }

        public Builder setImageResource(Map<Integer, Integer> mapping) {
            this.imageIntegerMap = mapping;
            return this;
        }

        public Builder setImageDrawable(Map<Drawable, Integer> mapping) {
            this.imageDrawableMap = mapping;
            return this;
        }

        public Builder setTextString(Map<String, Integer> mapping) {
            this.textStringMap = mapping;
            return this;
        }

        public Builder setTextResource(Map<Integer, Integer> mapping) {
            this.textIntegerMap = mapping;
            return this;
        }

        @Override
        protected CommonDialog createDialog() {
            CommonDialog commonDialog = new CommonDialog(mContext);
            commonDialog.setLayoutId(layoutId);
            commonDialog.setViewBinder(viewBinder);
            commonDialog.setDismissResult(dismissResult);
            commonDialog.setClickViewIds(clickViewIds);
            commonDialog.setOnViewClickListener(onViewClickListener);
            commonDialog.setImageBitmapMap(imageBitmapMap);
            commonDialog.setImageDrawableMap(imageDrawableMap);
            commonDialog.setImageResourceMap(imageIntegerMap);
            commonDialog.setTextStringMap(textStringMap);
            commonDialog.setTextResourceMap(textIntegerMap);
            return commonDialog;
        }
    }
}
